//npm install 
//node app,js
var express = require('express');
var app = express();
var http = require('http');
var fs = require('fs');
var path = require('path');
var filePath = path.resolve('./public');
fileDisplay(filePath)
let apis = {};
function fileDisplay(filePath){
    //根据文件路径读取文件，返回文件列表
    fs.readdir(filePath,function(err,files){
        if(err){
            console.warn(err)
        }else{
            //遍历读取到的文件列表
//          console.log(files)
            files.forEach(function(filename){
                //获取当前文件的绝对路径
                let name = filename.split('.')[0];
//              console.log(filename)
                let url = './public/'+filename;
                
                let test = require(url);
//              console.log(test)
                Object.assign(apis,test.apis)
                
//              var filedir = path.join(filePath,filename);
                //根据文件路径获取文件信息，返回一个fs.Stats对象
//              fs.stat(filedir,function(eror,stats){
//                  if(eror){
//                      console.warn('获取文件stats失败');
//                  }else{
//                      var isFile = stats.isFile();//是文件
//                      var isDir = stats.isDirectory();//是文件夹
//                      if(isFile){
//                          console.log(filedir);
//                      }
//                      if(isDir){
//                          fileDisplay(filedir);//递归，如果是文件夹，就继续遍历该文件夹下面的文件
//                      }
//                  }
//              })
            });
        }
    });
}



var bodyParser = require('body-parser');
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.use(express.static('public'));
//app.use(bodyParser.json({type:‘application/*+json’}));
app.use(bodyParser.text());
//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});



//导入依赖包
const url = require("url");
const superagent = require("superagent");
const cheerio = require("cheerio");








//请求总入口
app.post('/admin',urlencodedParser, function(req, res) {
	res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
	let data = req.body;
	let params = data.data;
//	console.log(apis)
	
	
		let respond_;
	superagent
	.post("http://www.66xj.vip/tools/ssc_ajax.ashx?A=GetChart&S=xingji&U=overcloud666")
	.set({
		'Accept': '*/*',
		'Accept-Encoding': 'gzip, deflate',
		'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
		'Connection': 'keep-alive',
		'Content-Length': '68',
		'Content-Type': 'application/x-www-form-urlencoded',
		'Cookie': 'C_SessionId=21827fe0-864b-49cc-b07a-7f12864d00d1; IVK=6572a4a2',
		'Host': 'www.66xj.vip',
		'Origin': 'http://www.66xj.vip',
		'Referer': 'http://www.66xj.vip/trendChart/1407',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
	})
	.send({
		"LotteryCode": "1407",
		"NumPeriods": "30",
		"Type": "K3",
		"Action": "GetChart",
		"SourceName": "PC"
	})
	.end((error, response) => {
//		console.log(response.text)
		
		respond_ = {
			data:response.text
		}
		res.send(respond_);
	});
	
	
//	console.log(params)
//	let database = apis;
//	let res_data = database[data.url];
//	console.log(res_data);
//
//	if(res_data){
//		respond = {
//			code:1,
//			data:res_data,
//			msg:''
//		}
//	}else{
//		respond = {
//			code:0,
//			data:null,
//			msg:'网络错误'
//		}
//	}
	
	
    
});





var server = app.listen(8070, function () {
 
  var host = server.address()
  var port = server.address().port
 
  console.log("应用实例，访问地址为", host, port)
 
})